$(function () {
  let pageCount = 1;
  let arr = location.search.split('&');
  let category = decodeURI(arr[1].split('=')[1]);
  
  let id = arr[0].split('=')[1];

  $('#category').text(category);

  function renderFir(pageid) {
    $.ajax({
      url: "http://localhost:3000/api/getproductlist",
      data: {
        pageid: pageid,
        categoryid: id
      },
      success: function (res) {
        // console.log(res);
        res.result.txt = category;
        $('.list-content').html(template('tmp', res.result));
        pageCount = Math.ceil(res.result.count / 10)
        // console.log(pageNum);
        var arrNum = []
        for (let i = 0; i < pageCount; i++) {
          arrNum.push(i);
        }
        // console.log(arrNum);
        $('#selectPage').html(template('tmp1', { list: arrNum }))
      }
    });

  }
  renderFir(1);

  function render(pageid) {
    $.ajax({
      url: "http://localhost:3000/api/getproductlist",
      data: {
        pageid: pageid,
        categoryid: id
      },
      success: function (res) {
        res.result.txt = category;
        // console.log(res.result);
        $('.list-content').html(template('tmp', res.result));
      }
    });

  }


  //分页器
  $('#selectPage').change(() => {
    // console.log($('#selectPage').val());
    let id = $('#selectPage').val();
    render(id);
  });

  $("#pageAdd").click(function () {
    if ($('#selectPage').val() == 1) {
      return
    }

    $('#selectPage').val(+$('#selectPage').val() - 1);
    let id = $('#selectPage').val();
    render(id);
  });
  $("#pageRemove").click(function () {
    if ($('#selectPage').val() == pageCount) {
      return
    }
    $('#selectPage').val(+$('#selectPage').val() + 1);
    let id = $('#selectPage').val();
    render(id);
  });
})